﻿using IBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HoomController : ControllerBase, IHoomBll
    {
        public HoomController(IHoomBll bll,ILogger<HoomController> logger)
        {
            Bll = bll;
            Logger = logger;
        }

        public IHoomBll Bll { get; }
        public ILogger<HoomController> Logger { get; }

        //添加
        [HttpPost]
        public async Task<int> AddHoom(Hoom hoom)
        {
            try
            {
                Logger.LogInformation("添加成功");
                return await Bll.AddHoom(hoom);
            }
            catch (Exception)
            {
                Logger.LogError("添加失败");
                throw;
            }
            
        }
        //下拉框
        [HttpGet]
        public List<HoomType> GetHoomType()
        {
            return Bll.GetHoomType();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="hoomNumber">编号</param>
        /// <param name="typeId">类型</param>
        /// <param name="createDate">创建时间</param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetHoom(string? hoomNumber, int typeId, DateTime? createDate, int index, int size)
        {
            return Bll.GetHoom(hoomNumber, typeId, createDate, index, size);
        }
        //删除
        [HttpDelete]
        public async Task<int> DelHoom(int id)
        {
            try
            {
                var value = await Bll.DelHoom(id);
                if (value > 0)
                {
                    Logger.LogInformation("删除成功");
                }
                else
                {
                    Logger.LogError("删除失败");
                }
                return value;
            }
            catch (Exception)
            {
                //记录日志
                string time=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"删除异常";
                Logger.LogError(time);
                throw;
            }
            
        }
        //反填
        [HttpGet]
        public Hoom FanTian(int id)
        {
            return Bll.FanTian(id);
        }
        //修改
        [HttpPut]
        public async Task<int> UpdaHoom(Hoom hoom)
        {
            return await Bll.UpdaHoom(hoom);
        }
    }
}
